export const findCurrentMenu = function(e) {
  let node = e.target
  while (node !== document.body) {
    if (node.classList.contains('wanxiang-menu-name')) {
      return node
    }
    node = node.parentElement
  }
}

export const parseJSON = function(text) {
  try {
    return JSON.parse(text)
  } catch {
    return null
  }
}

export const isCurrentPath = function(path) {
  return (window.location.pathname + window.location.hash.split('?')[0]) === path
}

function flattenMenu(menus, arr, parent) {
  menus.forEach(i => {
    i.parent = parent
    arr.push(i)
    if (i.children && i.children.length) {
      flattenMenu(i.children, arr, i)
    }
  })
}

export const getCurrentPath = function(menus, save = false) {
  const cur = window.location.pathname + window.location.hash.split('?')[0]
  let arr = []
  let exp = []
  let curMenu = null

  flattenMenu(menus, arr)

  for (let i = 0; i < arr.length; i++) {
    if (arr[i].path === cur) {
      curMenu = arr[i]
      break
    }
  }

  while (curMenu) {
    if (curMenu.code) {
      exp.unshift(curMenu.code)
    }
    curMenu = curMenu.parent
  }

  if (save) {
    window.localStorage.setItem('wanxiang_current_path', cur)
    window.localStorage.setItem('wanxiang_expand_menus', JSON.stringify(exp))
    window.localStorage.setItem('wanxiang_scroll_position', '0,0')
  } else {
    window.localStorage.removeItem('wanxiang_current_path')
    window.localStorage.removeItem('wanxiang_expand_menus')
    window.localStorage.removeItem('wanxiang_scroll_position')
  }

  return [cur, exp]
}

function updateExpandMenus() {
  const codes = []
  const items = window.document.getElementById('__wanxiang_menu').querySelectorAll('.wanxiang-menu-item')
  for (let item of items) {
    if (item.dataset.code && !item.classList.contains('wanxiang-colsed')) {
      codes.push(item.dataset.code)
    }
  }
  window.localStorage.setItem('wanxiang_expand_menus', JSON.stringify(codes))
}

export const toggleMenu = function(current) {
  const parent = current.parentElement
  const submenu = parent.querySelector('.wanxiang-submenu')
  if (submenu) {
    if (parent.classList.contains('wanxiang-colsed')) {
      // 展开
      submenu.style.maxHeight = submenu.dataset.maxHeight
      parent.classList.remove('wanxiang-colsed')
      updateExpandMenus()
      setTimeout(() => {
        submenu.style.removeProperty('max-height')
      }, 300)
    } else {
      // 收起
      const height = submenu.offsetHeight + 'px'
      submenu.style.maxHeight = height
      submenu.dataset.maxHeight = height
      setTimeout(() => {
        submenu.style.removeProperty('max-height')
        parent.classList.add('wanxiang-colsed')
        updateExpandMenus()
      })
    }

  }
}

export const iconArrow = function() {
  return '<i class="wanxiang-icon-arrow"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><path fill="currentColor" d="M831.872 340.864 512 652.672 192.128 340.864a30.592 30.592 0 0 0-42.752 0 29.12 29.12 0 0 0 0 41.6L489.664 714.24a32 32 0 0 0 44.672 0l340.288-331.712a29.12 29.12 0 0 0 0-41.728 30.592 30.592 0 0 0-42.752 0z"></path></svg></i>'
}
